package com.bm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bm.Do.Complain.ComplainAllotDo;
import com.bm.Do.Common.InfoDo;
import com.bm.Vo.Complain.ComplainAllotVo;
import com.bm.constant.Constants;
import com.bm.mapper.EjyyCommunityInfoMapper;
import com.bm.mapper.EjyyWechatMpUserMapper;
import com.bm.model.EjyyCommunityInfo;
import com.bm.model.EjyyComplain;
import com.bm.model.EjyyWechatMpUser;
import com.bm.query.Complain.ComplainAllotQuery;
import com.bm.service.EjyyComplainService;
import com.bm.mapper.EjyyComplainMapper;
import com.bm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
*
*/
@Service
public class EjyyComplainServiceImpl extends ServiceImpl<EjyyComplainMapper, EjyyComplain>
implements EjyyComplainService{

    @Autowired
    private EjyyComplainMapper complainMapper;

    @Autowired
    private EjyyCommunityInfoMapper communityInfoMapper;

    @Autowired
    private EjyyWechatMpUserMapper wechatMpUserMapper;

    /**
     * 投诉处理
     * @param complainAllotQuery
     * @return
     */
    @Override
    public ComplainAllotVo complainAllot(ComplainAllotQuery complainAllotQuery) {
        //根据id 和 community 还有步骤查询
        LambdaQueryWrapper<EjyyComplain> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EjyyComplain::getCommunityId,complainAllotQuery.getCommunityId());
        lqw.eq(EjyyComplain::getId,complainAllotQuery.getId());
        lqw.eq(EjyyComplain::getStep, Constants.SUBMIT_REPAIR_STEP);
        //调用查询
        EjyyComplain detail = complainMapper.selectOne(lqw);
        if(ObjectUtils.isEmpty(detail)){
            return null;
        }
        ComplainAllotDo disposedInfo = complainMapper.selectUserInfoById(complainAllotQuery.getDisposeUserId());
        if(ObjectUtils.isEmpty(disposedInfo)){
            return null;
        }
        //查询社区信息
        EjyyCommunityInfo communityInfo = communityInfoMapper.selectById(detail.getCommunityId());

        if(detail.getDisposeSubscribed()){
            //查询微信用户信息
            EjyyWechatMpUser wechatMpUserInfo = wechatMpUserMapper.selectById(detail.getWechatMpUserId());
            //发送信息给微信小程序 (后期处理)
        }
        if(disposedInfo.getSubscribed() && disposedInfo.getOpenId() != null){
            //发送短信给物业人员 尽快处理投诉信息
        }
        detail.setStep(Constants.ALLOT_REPAIR_STEP);
        //用户iD 目前未存入 先暂定为1
        detail.setAllotUserId(Constants.ADMIN_ID);
        detail.setAllotedAt(DateUtils.dateConversion());
        detail.setDisposeUserId(complainAllotQuery.getDisposeUserId());
        //调用mapper
        complainMapper.updateById(detail);
        //删除隐私数据
        disposedInfo.setOpenId(null);
        disposedInfo.setSubscribed(null);

        //封装返回对象
        ComplainAllotVo complainAllotVo = new ComplainAllotVo();
        complainAllotVo.setDisposedInfo(disposedInfo);
        InfoDo allotInfo = new InfoDo();
        //假数据 目前写死
        allotInfo.setId(Integer.parseInt(Constants.ADMIN_ID+""));
        allotInfo.setRealName("bm");
        complainAllotVo.setAllotInfo(allotInfo);
        complainAllotVo.setAllotedAt(DateUtils.dateConversion());

        return complainAllotVo;
    }
}
